<?php
include_once dirname(dirname(__FILE__)) . '/clases/fpdf/fpdf.php';
include_once dirname(dirname(__FILE__)) . '/filtro_admin.php';
include_once dirname(dirname(__FILE__)) . '/funciones/basicas.php';

if(isset($_GET['nfactura']))
{
$nfactura = $_GET['nfactura'];
$conexion = conectar();
$dias = "dias";
$pdf = new FPDF('P','mm','Letter');
$pdf->AddPage();
$pdf->SetFont('Arial','',11);
$sql="SELECT factura.*,cliente.*,factura.fecha_ingreso
      FROM factura,cliente 
	   WHERE factura.cod_factura='$nfactura'
	   AND cliente.cod_cli=factura.cod_cli";
$consulta = mysql_query($sql,$conexion)or die("Error");
if (!$consulta)  die ("Error en la consulta: ". mysql_error());
$nfilas = mysql_num_rows($consulta);
if($nfilas > 0)
{     
   $fila=mysql_fetch_array($consulta);
   $pdf->Text(30,30,FormatoFecha($fila['fecha_ingreso']));
   $pdf->Text(150,30,$fila['cod_cli']);
   $pdf->Text(150,5,$fila['cod_factura']);
   $pdf->Text(26,45,$fila['nombre']);
   $pdf->Text(133,55,$fila['rut']);
   $pdf->Text(26,55,$fila['direccion']);
   $pdf->Text(25,65,$fila['comuna']);
   $pdf->Text(70,65,$fila['ciudad']);
   $pdf->Text(120,65,$fila['telefono']);
   $pdf->Text(160,65,"Orden NO:");
   $pdf->Text(177,65,$fila['orden']);
   $pdf->Text(20,75,$fila['giro']);
   $pdf->Text(180,75,$fila['forma_pago']);
   $pdf->Text(195,75,$dias);
   $pdf->Text(180,210,number_format($fila['neto'],0,",","."));
   $pdf->Text(180,220,number_format($fila['iva'],0,",","."));
   $pdf->Text(180,230,number_format($fila['total'],0,",","."));
   $pdf->Text(20,210,$fila['total_palabras']);
   
}
$sql="SELECT producto_factura.*,producto.nombre 
      FROM producto_factura,producto 
	  WHERE producto_factura.cod_factura='$nfactura'
	  AND producto.cod_prod=producto_factura.cod_prod";
$consulta=mysql_query($sql,$conexion)or die("Error");
if (!$consulta)  die ("Error en la consulta: ". mysql_error());
$nfilas=mysql_num_rows($consulta);
if($nfilas>0)
{
    $j=100;
	for($i=0;$i<$nfilas;$i++)
	{
	  $fila=mysql_fetch_array($consulta);
	  $pdf->Text(10,$j,$fila['cantidad']);
	  $pdf->Text(35,$j,$fila['nombre']);
	  $pdf->Text(150,$j,number_format($fila['precio_venta'],0,",","."));
	  $pdf->Text(180,$j,number_format($fila['neto'],0,",","."));
	  $j=$j+5;
	}
}
$pdf->Output("Factura_" . $nfactura. ".pdf" , "D");
mysql_close($conexion);
}
?>